package com.ptt.sqm.ws;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.PropertyInfo;

import android.annotation.SuppressLint;
import android.util.Log;

import com.ptt.sqm.util.AppUtil;

/**
 * @author AndroidExample DotNetWebService Class
 * 
 */
@SuppressLint("NewApi")
public class GeocoderJSONCall implements IDefaulCall{

	private static GeocoderJSONCall singleton;

	private String url;
	
	public AsyncResponse delegate = null;

	public static GeocoderJSONCall getInstance(String url) {
		if (singleton == null) {
			singleton = new GeocoderJSONCall(url);
		}

		return singleton;
	}

	public GeocoderJSONCall(String url) {
		this.url = url;
	}

	public String execute(String method, PropertyInfo[] params) {
		HttpGet httpGet = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		HttpResponse response;
		StringBuilder stringBuilder = new StringBuilder();
		InputStream stream = null;
		String responseBody = "";
		Log.i("JSON CALL", url);
		try {
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			
			responseBody = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);			
		} catch (ClientProtocolException e) {
			Log.e("JSON CALL", e.getMessage());
		} catch (IOException e) {
			Log.e("JSON CALL",  e.getMessage());
		} finally {
			
		}
		return responseBody;
	}

	public void setLatLng(String arg, double lat, double lng) {
		StringBuilder latLng = new StringBuilder();
		latLng.append(url);
		latLng.append(arg);
		latLng.append(String.valueOf(lat));
		latLng.append(AppUtil.COMMA);
		latLng.append(String.valueOf(lng));
		url = latLng.toString();
	}

}
